<template>
  <uni-nav-bar title="任务" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <!-- 切换 -->
  <z-tabs ref="tabs" :list="list" :current="current" @change="tabsChange" inactive-color="#333" />
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package_sale" v-for="item in orderList" :key="item.id">
      <view class="space-between">
        <view class="center">
          <text style="font-weight: 500;">订单号：{{item.order_sn}}</text>
        </view>
        <view class="but">{{item.status_text?item.status_text:item.status_name}}</view>
      </view>
      <view class="center top">
        <view class="left">配送地址</view>
        <view class="center" style="align-items: flex-start;width:75%;">
          {{item.address?item.address:item.delivery_address}}
        </view>
      </view>
      <view class="center top">
        <view class="left">预约时间</view>
        <view>{{item.deliverytime?item.deliverytime:item.delivery_time}}</view>
      </view>
      <view class="center top" v-if="item.starttime">
        <view class="left">送达时间</view>
        <view>{{item.endtime}}</view>
      </view>
      <view class="center top">
        <view class="left">车牌</view>
        <view>{{item.cart_code}}</view>
      </view>
      <view class="center top">
        <view class="left">车友</view>
        <view>{{item.driver_name}} <text style="color:#3F8FFB;"
            @click="telCall(item.driver_mobile)">{{item.driver_mobile}}</text></view>
      </view>
      <view class="center top">
        <view class="left">备注</view>
        <view class="center" style="width:70%;align-items: flex-start;">
          {{item.des?item.des:'备注'}}
        </view>
      </view>
      <view class="center top">
        <view class="left">福利品</view>
        <view v-if="item.fuli_nums!=0" @click="current===0?operate(item.id,6):operate(item.subscribe_id,6)">
          {{item.fuli_nums}} >>
        </view>
        <view v-else>暂无福利品</view>
      </view>
      <view class="center top">
        <view class="left">进展</view>
        <view @click="operate(item.id,4,item.is_show)">{{item.status_text?item.status_text:item.status_name}}
          <uni-icons :type="item.is_show===1?'bottom':'top'" size="20"></uni-icons>
        </view>
      </view>
      <view class="center top" v-if="item.is_show===1">
        <view class="left"></view>
        <uni-section title="纵向排列" type="line" padding>
          <uni-steps :options="item.log_list" active-color="#007AFF" :active="active" direction="column" />
        </uni-section>
      </view>
    </view>
    <Popu v-if="orderList.length<1" :pageHeight="pageHeight" text="暂无任务"></Popu>
  </scroll-view>
  <!-- 福利列表 -->
  <CarShop ref="shopList"></CarShop>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import Popu from '@c/earthPushing/common/popu.vue';
  import { ref } from 'vue';
  import { toPublish } from '@mqtt';
  import { telCall } from '@/utils/copyText';
  import CarShop from '@c/reservation/carShop.vue';
  import { getTaskList } from '@/gql/earthPushing/dispatch';
  import { onLoad } from '@dcloudio/uni-app';
  import { showLoading } from '@/utils/prompt';
  const staff_id = ref()
  const list = [{ name: '新任务', type: 1 }, { name: '配送中', type: 2 }, { name: '已完成', type: 3 }]
  const active = 0 //纵向排列首位高亮
  const current = ref(0) //tab选中当前索引信息
  const tabs = ref(null) //tab切换组件
  const shopList = ref(null) //福利品列表弹框
  const pageHeight = ref();

  const orderList = ref([])
  const lastPage = ref(1);
  const currentPage = ref(1);

  const type = ref(1)

  const orderID = ref() //选中的当前订单ID
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 100;
    },
  });
  onLoad((pearm) => {
    staff_id.value = Number(pearm.id)
    init()
  })
  function init() {
    showLoading()
    const payload = {
      query: getTaskList,
      variables: {
        page: currentPage.value,
        status: type.value,
        staff_id: staff_id.value,
      },
    };
    toPublish(
      'ql/staff/getTaskList',
      payload,
      (obj : any) => {
        uni.hideLoading();
        const { getTaskList } = obj.data;
        orderList.value = [...orderList.value, ...getTaskList.list];
        lastPage.value = getTaskList.lastpage;
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  //tabs通知swiper切换
  function tabsChange(index : number) {
    current.value = index;
    type.value = list[index].type
    currentPage.value = 1
    orderList.value = []
    init();
  }
  function operate(id : number, type : number, show : number) {
    orderID.value = id
    switch (type) {
      case 4:
        const num = show === 0 ? 1 : 0
        orderList.value.filter((item) => item.id === id ? item.is_show = num : item)
        break;
      case 6:
        shopList.value.init(id)
        break;
    }
  }
</script>

<style scoped lang="less">
  .state {
    padding: 2rpx 10rpx;
    box-sizing: border-box;
    background: rgba(164, 202, 255, .3);
    border-radius: 30px;
    color: #3F8FFB;
    font-size: 24rpx;
    margin-left: 10rpx;
  }

  .top {
    margin-top: 15rpx;
    align-items: flex-start;

    .left {
      width: 22%;
      color: #666;
    }
  }

  .but {
    width: 140rpx;
    height: 57rpx;
    background: rgba(164, 202, 255, .2);
    border-radius: 5px;
    text-align: center;
    line-height: 50rpx;
    color: #3F8FFB;
  }

  .bottoms {
    justify-content: flex-end;
    margin-top: 20rpx;

    view {
      width: 166rpx;
      height: 64rpx;
      border: 1px solid #999999;
      border-radius: 5px;
      text-align: center;
      line-height: 64rpx;
      box-sizing: border-box;
      margin-left: 20rpx;
    }

    view:nth-of-type(3) {
      border: 0px;
      background: #1D8DFF;
      color: #fff;
    }
  }

  .popup {
    width: 100%;

    view {
      width: 100%;
      padding: 25rpx 0px;
      box-sizing: border-box;
      background-color: white;
      text-align: center;
      font-size: 32rpx;
    }

    view:nth-of-type(2) {
      border-bottom: 1px solid #eee;
      border-top: 1px solid #eee;
    }

    view:nth-last-child(1) {
      margin-top: 20rpx;
    }
  }

  .bnt {
    width: 108rpx;
    height: 36rpx;
    border: 1px solid #1B91FF;
    border-radius: 18rpx;
    text-align: center;
    line-height: 36rpx;
    font-size: 20rpx;
    color: #1D8DFF;
  }
</style>
<style>
  .top>>>.uni-steps__column-text {
    border-bottom: 0px;
  }

  .top>>>.uni-steps__column-title {
    font-size: 28rpx;
  }

  .top>>>.uni-steps__column-desc {
    font-size: 26rpx;
    margin-top: 5rpx;
  }

  .bottoms>>>.uni-date {
    width: 166rpx;
    flex: none;
  }
</style>